<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html lang="en">

<head>
	<base href="<%=basePath%>">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>新建处方单</title>
    <link rel="stylesheet" href="static/css/common.css">
    <link rel="stylesheet" href="static/css/bootstrap.css">
    <link rel="stylesheet" href="static/css/font-awesome-4.7.css">
    <link rel="stylesheet" href="static/css/cf.css">
    <script src="static/js/jquery-1.11.3.js"></script>
    <script src="static/js/bootstrap.js"></script>
    <script src="static/layer/layer.js"></script>
    <!--提示框-->
	<script type="text/javascript" src="static/js/jquery.tips.js"></script>
    <script type="text/javascript">
			$(top.hangge());
	</script>
	<style>
    	.table th, .table td {
    		text-align:center;    		
    		vertical-align: middle;
    	}
    	form {
			margin: 0 10px 2px;
		}
    </style>    
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 患者主要信息展示页面 -->
            <div class="col-sm-offset-2 col-sm-8 col-md-offset-2 col-md-8 col-lg-offset-2 col-lg-8  main shadow">
                <form action="" id="cfCardForm" >
                    <!-- 患者信息 -->
                    <div class="r1">
                        <div class="col-sm-2 col-md-2 col-lg-2 lf left ">
                            <i>*</i>
                            <span>姓名:</span>
                        </div>
                        <div class="col-sm-10 col-md-10 col-lg-10 lf right">
                            <!-- 姓名输入框 -->
                            <div class="col-sm-6 col-md-6 col-lg-6 box">
                                <input type="text" class="name" placeholder="请输入患者姓名" value="${caseOrder.name }" readonly="readonly">
                            </div>
                            <!-- 性别选择 -->
                            <div class="col-sm-6 col-md-6 col-lg-6 sex">
                                <div class="left lf">
                                    <i>*</i>
                                    <span>姓别:</span>
                                </div>
                                <div class="right">
                                    <label id="sex">${caseOrder.sex }</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- 年龄及联系方式 -->
                    <div class="r2">
                        <div class="col-sm-2 col-md-2 col-lg-2 lf left ">
                            <i>*</i>
                            <span>年龄:</span>
                        </div>
                        <div class="col-sm-10 col-md-10 col-lg-10 lf right">
                            <!-- 年龄输入框 -->
                            <div class="col-sm-6 col-md-6 col-lg-6 box">
                                <input type="text" class="age" placeholder="请输入患者年龄" value="${caseOrder.age }" readonly="readonly">&nbsp;&nbsp; 岁
                            </div>
                            <!-- 联系方式 -->
                            <div class="col-sm-6 col-md-6 col-lg-6">
                                <div class="left lf">
                                    <i>*</i>
                                    <span>电话:</span>
                                </div>
                                <div class="right">
                                    <input type="text" class="phone" placeholder="请输入您的联系方式" value="${caseOrder.phone }" readonly="readonly">
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- 医生主诉 -->
                    <div class="r7">
                        <div class="col-sm-2 col-md-2 col-lg-2 lf left ">
                            <span>主诉/现病史:</span>
                        </div>
                        <div class="col-sm-10 col-md-10 col-lg-10 lf right">
                            <input type="text" class="yszs xbs" placeholder="请输入病人病史" value="${caseOrder.xbs }" readonly="readonly">
                        </div>
                    </div>
                    <!-- 医生诊断 -->
                    <div class="r7">
                        <div class="col-sm-2 col-md-2 col-lg-2 lf left ">
                            <i>*</i>
                            <span>诊断:</span>
                        </div>
                        <div class="col-sm-10 col-md-10 col-lg-10 lf right">
                            <input type="text" class="yszd zd" placeholder="请输入诊断结果" value="${caseOrder.zd }" readonly="readonly">
                        </div>
                    </div>
                    <!-- 处方 -->
                    <div class="cf">
                        <div class="col-sm-2 col-md-2 col-lg-2 lf left ">
                            <span>处方:</span>
                        </div>
                        <div class="col-sm-10 col-md-10 col-lg-10 lf right">
                            <div class="zrcf">
                                <a class="btn btn-info"  id="load">载入常用处方</a>
                                <a style="display: none" class="btn btn-info" data-toggle="modal" data-target="#ceshi" id="load2">载入常用处方</a>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover" id="table4">
                                    <tr>
                                        <td>序号</td>
                                        <td>编号</td>
                                        <td>名称</td>
                                        <td>单位</td>
                                        <td>用法</td>
                                        <td>单价</td>
                                        <td>用量</td>
                                        <td>总价</td>
                                    </tr>
                                </table>  
                                <div class="rt">
                                	<a class="btn btn-default" style="margin-top:-10px;margin-bottom:10px;" id="addCycf">设为常用处方</a>
                                	<a class="btn btn-default" style="margin-top:-10px;margin-bottom:10px;" id="chooseYp">添加药品</a>
                                    <a class="btn btn-default" style="margin-top:-10px;margin-bottom:10px;" id="clear">清空内容</a>   
                                </div>                          
                            </div>
                        </div>
                    </div>
                    <!-- 患者医嘱 -->
                    <div class="r8">
                        <div class="col-sm-2 col-md-2 col-lg-2 lf left ">
                            <span>医嘱:</span>
                        </div>
                        <div class="col-sm-10 col-md-10 col-lg-10 lf right">
                            <textarea name="" id="yizhu" cols="30" rows="4" placeholder="请输入医嘱信息" class="textarea"></textarea>
                        </div>
                    </div>
                    <!-- 收货人信息 -->
                    <div class="r6">
                        <div class="col-sm-2 col-md-2 col-lg-2 lf left ">
                            <span>收货人信息:</span>
                        </div>
                        <div class="col-sm-10 col-md-10 col-lg-10 lf right">
                            <input type="text" class="consignee">
                        </div>
                    </div>
                    <!-- 价格结算 -->
                    <div class="r5">
                        <div class="col-sm-offset-2 col-sm-10 col-md-offset-2 col-md-10 col-lg-offset-2 col-lg-10 js">
                            <ul class="ul">
                                <li class="lf ypje">
                                    药品金额：
                                    <span id="ypje">0</span>
                                </li>
                                <li class="lf zlfy">
                                    治疗费用：
                                    <input type="text" class="fy" id="zlfy" value="0">元
                                </li>
                                <li class="rt">
                                    合计金额：
                                    <span>￥&nbsp;
                                        <b id="hjje">0</b>元</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <!-- 表单提交 -->
                    <div class="bottom col-sm-offset-5 col-sm-2 col-md-offset-5 col-md-2 col-lg-offset-5 col-lg-2">
                        <a class="btn btn-info" id="sub" style="width:100%;">提交</a>
                        <input type="hidden" name="hzId" value="${caseOrder.hzId }" class="hzId">
                        <input type="hidden" name="id" value="${caseOrder.id }" class="id">
                        <input type="hidden" name="bldCode" value="${caseOrder.bldCode }" class="bldCode">
                    </div>
                </form>
            </div>
        </div>
    </div>
 
<!--     固定定位的登录模态框 -->
    <div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static" aria-labelledby="gridSystemModalLabel"
        id="ceshi">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="false">&times;</span>
                    </button>
                    <p class="modal-title" id="gridSystemModalLabel">常用处方选择</p>
                </div>
                <div class="modal-body">
                
                    <form class="form-horizontal" action="">
                        <div class="form-group">
                            <div class="col-sm-12">
                            	<div class="content col-xs-12 col-md-12 col-sm-12">
                            	
                                    <div class="col-xs-3 col-md-3 col-sm-3 lefts" id="table5">
                                        <ul class="uls" id="table5ul">
                                        </ul>
                                    </div>
                                    
                                    <div class="col-xs-9 col-md-9 col-sm-9 right" style="padding:3px;">
                                        <div class="nr" >
											<table class="table table-bordered table-condensed" id="table6">
			                                    <tr style="background-color:#eee;">
                                                    <th style="width: 160px;">编号</th>
			                                        <th style="width:200px;">药品名称</th>
			                                        <th style="width:80px;">单位</th>
			                                        <th style="width:80px;">用量</th>
			                                        <th style="width:1000px;">用法</th>
                                                </tr>
			                                </table>
                                        </div>
                                    </div>
                                    
                                </div>
                              
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-danger" data-dismiss="modal" style="padding-left: 5px;padding-right: 5px;" id="delCycf" >删除</button>
                            <button type="button" class="btn btn-primary" data-dismiss="modal" style="padding-left: 5px;padding-right: 5px;" id="addCycfyp" >确定</button>
                        </div>
                    </form>
                    
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">
		$(function(){
				var _reg = /^\d{0,10}(.\d{1,2})?$/;
				var _loadCycfId ;
				//1 ：加载常用处方------------------------开始
				$("#load").click(function(){
					_loadCycfId = -1 ;
					$("#table5ul li").remove();			//删除左侧菜单
		     		$.ajax({
		    			type: "POST",
						url: "<%=basePath%>cycf/cycfList.do",
						cache: false,
						success: function(data){
							 if(data.msg=="success"){
							 	for ( var int = 0; int < data.cycfList.length; int++) {
									$("#table5ul").append(
															"<li class='table5li'>"+
																"<input type='radio' name='test' id='cs" + (int+1)+"' value='"+ data.cycfList[int].id +"'>"+
																"<label for='cs" + (int+1) + "'>" +data.cycfList[int].cycfName+"</label>"+
															"</li>"	
									);
								}
								//$(".table5li:first").children("input").attr("checked","checked");
								$("#table5ul .table5li:first input").click();
							 	$("#load2").click();
							 }else{
							 	layer.alert(data.msg);
							 }
						 }
		    		}); 
				});		
				//1 ：加载常用处方-------------------------结束
				
				
				//1-1 ：点击常用处方，查询其所有药品的名称-----------开始
				$("#table5ul").on("click","li",function(){
					if(Number(_loadCycfId) == $(this).children("input").val() ){
					}else{
						$(this).siblings().removeClass("xz");
						$(this).addClass("xz");
						_loadCycfId = $(this).children("input").val();
						findCycfypNames(_loadCycfId);
					}
				});
				//1-1 ：点击常用处方，查询其所有药品的名称-----------结束
				
				
				//1-2 ：删除常用处方---------------------开始
	    		$("#delCycf").click(function(){
					var _radio = $("#table5ul li [name='test']:checked");
					if(_radio.length>0){
						var _cycfId = $(_radio).val();
						$.ajax({
							type: "POST",
							url: '<%=basePath%>cycf/delCycf.do',
						    data: {cycfId:_cycfId},
							dataType:'json',
							cache: false,
							success: function(data){
								if(data.msg=="success"){
									$(_radio).parent().remove();
								}else{
									layer.alert(data.msg);
								}
							}
						});
						
					}else{
						layer.alert("没有选！");
					}
	    		});	
				//1-2 ：删除常用处方---------------------结束
				
				
				//1-3 ：（确定）加载常用处方药品到处方单中----------开始
				$("#addCycfyp").click(function(){
					var _radio = $("#table5ul li [name='test']:checked");
					if(_radio.length>0){
						var _cycfId = $(_radio).val();
						$.ajax({
							type: "POST",
							url: '<%=basePath%>cycf/cycfypList.do',
						    data: {cycfId:_cycfId},
							dataType:'json',
							cache: false,
							success: function(data){
								if(data.cycfypList.length>0){
									var xuhao = $("#table4 tr:last td:first").text();
			    					if(xuhao=="序号"){
			    						xuhao = 0;
			    					}
			    					var _loadTotalPrice = 0;		//所有常用处方药品的总价和
									for ( var int = 0; int < data.cycfypList.length; int++) {
										xuhao = Number(xuhao) + Number(1);
						    		 	$("#table4").append("<tr class='tr'>"+
						    									"<td>"+xuhao+"</td>"+
						    									"<td>"+data.cycfypList[int].ypcode+"</td>"+
						    									"<td>"+data.cycfypList[int].ypname+"</td>"+
						    									"<td>"+data.cycfypList[int].ypunit+"</td>"+
						    									"<td>"+data.cycfypList[int].ypusage+"</td>"+
						    									"<td>"+data.cycfypList[int].price+"</td>"+			//单价
						    									"<td class='number'>"+data.cycfypList[int].number+"</td>"+			//用量
						    									"<td>"+data.cycfypList[int].totalprice+"</td>"+
						    									"<input type='hidden' value='"+data.cycfypList[int].id+"' class='ypId'>"+
					                                    	"</tr>"); 
					                     _loadTotalPrice = Number(_loadTotalPrice) + Number(data.cycfypList[int].totalprice);
									}
									//计算药品总价
					    			var ypje = $("#ypje").text();	//获取药品金额
					    			var hjje = $("#hjje").text();	//获取合计金额
					    			ypje =Number( (Number(ypje) + Number(_loadTotalPrice) ).toFixed(2) );		//药品金额 = 原药品金额 + 一个药品的总价
					    			hjje =Number( (Number(hjje) + Number(_loadTotalPrice) ).toFixed(2));			//合计金额 = 原合计金额 +一个药品的总价 
					    			$("#ypje").text(ypje);		//修改药品金额
					    			$("#hjje").text(hjje);		//修改合计金额
								}
							}
						});
						
					}else{
						layer.alert("没有选！");
					}
	    		});	
				//1-3 ：（确定）加载常用处方药品到处方单中----------结束
				
				
	    		//2.设为常用处方------------------------开始
	    		$("#addCycf").click(function(){
	    			var trs = $("#table4 .tr");  //获取tr集合
	    			if(trs.length>0){
	    				var _ypIds = '';
	    				var _numbers = '';
	    				for(var _i = 0 ; _i<trs.length ; _i++){
	    					if(_ypIds==''){
	    						_ypIds += $(trs[_i]).children(".ypId").val();
	    						_numbers += $(trs[_i]).children(".number").html();
	    					}else {
		    					_ypIds += ',' + $(trs[_i]).children(".ypId").val();
		    					_numbers += ',' + $(trs[_i]).children(".number").html();
	    					}
	    				}
		    			var cycf_name="";
		    			layer.prompt({
						  formType: 2,	//0:文本，1：密码，2：多行文本
						  value: "",
						  title: "请输入常用处方名称"
						}, function(value, index, elem){
							cycf_name = value;
						  layer.close(index);
						   if (value!=null && value.trim()!=""){
						   		//判断常用处方名称是否存在
			    				$.ajax({
									type: "POST",
									url: '<%=basePath%>cycf/hasByCycfName.do',
							    	data: {cycfName:cycf_name.trim()},
									dataType:'json',
									cache: false,
									success: function(data){
										if(data.result=="yes"){
											layer.alert("该名称已存在");
										}else{
						    				//不存在就添加
						    				$.ajax({
												type: "POST",
												url: '<%=basePath%>cycf/add.do',
										    	data: {ypIds:_ypIds,numbers:_numbers,cycfName:cycf_name},
												dataType:'json',
												cache: false,
												success: function(data){
													if(data.msg=="success"){
														layer.alert("添加成功！");
													}else{
														layer.alert(data.msg);
													}
												}
											});
										}
									}
								});
						   }else{
						   	layer.alert("名称不能为空");
						   }
						});
	    			}else{
	    				layer.alert("请先添加药品");
	    			}
	    		});		
	    		//2.设为常用处方--------------------结束
	    		
	    		
	    		//3.添加药品------------------------开始
	    		$("#chooseYp").click(function(){
		    			//追加药品
						top.jzts();	//显示加载进度
						 var diag = new top.Dialog();
						 diag.Drag=true;
						 diag.Title ="新增";
						 diag.URL = '<%=basePath%>cfOrder/chooseYp.do';
				 		 diag.Width = 1000;
						 diag.Height = 800; 
						 diag.CancelEvent = function(){ //关闭事件
						 	if(window.top.selectYp=="has"){
			    					//序号
			    					var xuhao = $("#table4 tr:last td:first").text();
			    					if(xuhao!="序号"){
			    						xuhao = Number(xuhao) + Number(1);
			    					}else{
			    						xuhao = 1;
			    					}
					    			var zj = (Number(window.top.price)*Number(window.top.number)).toFixed(2);	//一个药品总价
					    		 	$("#table4").append("<tr class='tr'>"+
					    									"<td>"+xuhao+"</td>"+
					    									"<td>"+window.top.ypCode+"</td>"+
					    									"<td>"+window.top.ypName+"</td>"+
					    									"<td>"+window.top.ypUnit+"</td>"+
					    									"<td>"+window.top.ypUsage+"</td>"+
					    									"<td>"+window.top.price+"</td>"+			//单价
					    									"<td class='number'>"+window.top.number+"</td>"+			//用量
					    									"<td>"+zj+"</td>"+
					    									"<input type='hidden' value='"+window.top.ypId+"' class='ypId'>"+
				                                    	"</tr>"); 
				                    //计算药品总价
					    			var ypje = $("#ypje").text();	//获取药品金额
					    			var hjje = $("#hjje").text();	//获取合计金额
					    			ypje =Number( (Number(ypje) + Number(zj) ).toFixed(2) );		//药品金额 = 原药品金额 + 一个药品的总价
					    			hjje =Number( (Number(hjje) + Number(zj) ).toFixed(2));			//合计金额 = 原合计金额 +一个药品的总价 
					    			$("#ypje").text(ypje);		//修改药品金额
					    			$("#hjje").text(hjje);		//修改合计金额
								}
						 //	layer.alert(window.top.selectYp.ypmc);
							 if(diag.innerFrame.contentWindow.document.getElementById('userForm').style.display == 'none'){
								 if('${page.currentPage}' == '0'){
									 top.jzts();
									 setTimeout("self.location=self.location",100);
								 }else{
									 nextPage(${page.currentPage});
								 }
							}
							diag.close();
						 };
						 diag.show();
	    		}); 	
	    		//3.添加药品----------------------------结束
	    		
	    		
	    		//4.清除处方药品内容---------------------开始
	    		$("#clear").click(function(){
	    			$("#table4 tr:gt(0)").remove();		//清除所有添加的药品
	    			$("#ypje").text(0);					//药品价格清零
	    			//计算合计金额
	    			var zlfy = $("#zlfy").val();
	    			if(_reg.test(zlfy)){
	    				$("#hjje").text(zlfy);
	    			}else{
	    				$("#hjje").text(0);
	    			}
	    		});		
	    		//4.清除处方药品内容--------------------结束
	    		
	    		
	    		//5.治疗费用失去焦点时，验证输入的内容--------开始
	    		$("#zlfy").blur(function(){
	    			var zlfy = $("#zlfy").val();
	    			if(_reg.test(zlfy)){
	    				var ypje = $("#ypje").text();
	    				var hjje =  Number((Number(ypje) + Number(zlfy)).toFixed(2));
	    				 $("#hjje").text(hjje);
	    			}else{
	    				$("#zlfy").val("0");
	    				layer.alert("格式不正确");
	    			}
	    		});
	    		//5.治疗费用失去焦点时，验证输入的内容--------结束
	    		
	    		
	    		//6.表单提交--------------------------------开始
	    		$("#sub").click(function(){
	    			//获取处方中的每一药品，做添加
	    			var trList = $("#table4 tr:eq(0)").nextAll();		//获取药品tr集合
	    			var arrayList = new Array(trList.length);
	    			for(var i = 0 ; i<trList.length ; i++){
	    				var child = $(trList[i]).children();			//获取药品tr下的集合
	    			 	var yp = new Array(4);
	    				//获取单价、用量、总价、药品id
	    				for(var j = 5 ,x=0; j<child.length-1; j++,x++){
	    					yp[x] = $(child[j]).text();
	    				}
	    				yp[3] = $(child[child.length-1]).val();
	    				arrayList[i] = yp ;
	    			} 
	    			//医嘱、药品金额、治疗费用、合计金额、患者ID、病历单id
	    			var yizhu = $("#yizhu").text();
	    			var ypje = $("#ypje").text();
	    			var zlfy = $("#zlfy").val();
	    			var hjje = $("#hjje").text();
	    			var hzid = $(".hzId").val();
	    			var caseid = $(".id").val();
	    			var bldId = $(".bldId").val();
	    			$.ajax({
	    				type: "POST",
						url: "<%=basePath%>cfOrder/save.do",
				    	data: {
				    			"hzId":hzid,
				    			"caseId":caseid,
				    			"bldCode":bldId,
				    			"yiZhu":yizhu,
				    			"ypje":ypje,
				    			"zlfy":zlfy,
				    			"hjje":hjje,
				    			"ypList":JSON.stringify(arrayList)
				    		},
						dataType:'json',
						//beforeSend: validateData,
						cache: false,
						success: function(data){
							if(data.msg=="success"){
								layer.msg("操作成功！", {
								  icon: 1,
								  time: 2000 //2秒关闭（如果不配置，默认是3秒）
								}, function(){
									window.location.reload(true);
								});   
								//layer.alert("操作成功");
							}else{
								layer.msg("操作失败！", {
								  icon: 2,
								  time: 2000 //2秒关闭（如果不配置，默认是3秒）
								});
							}       
						 }
	    			});
	    		
	    		});	
	    		//6.表单提交--------------------------------结束
	    		
		});
		
		
   		//7.方法：根据常用处方ID，查询其所有药品的名称-----------开始
   		function findCycfypNames(loadCycfId){
   				$("#table6 .table6tr").remove();
				$.ajax({
					type: "POST",
					url: '<%=basePath%>cycf/cycfypList.do',
				    data: {cycfId:loadCycfId},
					dataType:'json',
					cache: false,
					success: function(data){
						if(data.cycfypList.length>0){
							for( var j = 0 ; j < data.cycfypList.length ; j++ ){
								$("#table6").append(
														"<tr class='table6tr'>"+
																"<td>"+ data.cycfypList[j].ypcode+"</td>"+
																"<td>"+ data.cycfypList[j].ypname +"</td>" +
																"<td>"+ data.cycfypList[j].ypunit+"</td>"+
																"<td>"+ data.cycfypList[j].number+"</td>"+
																"<td>"+ data.cycfypList[j].ypusage+"</td>"+
														"</tr>");
/* 								$("#table6 tr").last().append( ); */
							}
						}
					}
				});
   		}
   		//7.方法：根据常用处方ID，查询其所有药品的名称-----------结束
   
	</script> 
</body>

</html>
